package com.explorati.LeetCode006.reversePrint;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @ Author : Weijian_Wang
 * @ Date : Created in 18:11 2020/9/13 0013
 * @ Description ：
 */
public class Solution {
    public static class ListNode {
      int val;
      ListNode next;
      ListNode(int x) { val = x; }
    }

    //输入一个链表的头节点，从尾到头反过来返回每个节点的值（用数组返回）
    public static int[] reversePrint(ListNode head) {
        Stack<Integer> stack = new Stack<Integer>();

        while(head != null) {
            stack.push(head.val);
            head = head.next;
        }
        int size = stack.size();
        int[] array = new int[size];
        for(int i = 0; i < size; i ++) {
            array[i] = stack.pop();
        }
        return array;

//        Stack<ListNode> stack = new Stack<ListNode>();
//        ListNode temp = head;
//        while (temp != null) {
//            stack.push(temp);
//            temp = temp.next;
//        }
//        int size = stack.size();
//        int[] print = new int[size];
//        for (int i = 0; i < size; i++) {
//            print[i] = stack.pop().val;
//        }
//        return print;

    }

    public static void main(String[] args) {
        ListNode l1 = new ListNode(1);
        ListNode l2 = new ListNode(3);
        ListNode l3 = new ListNode(2);

        l1.next = l2;
        l2.next = l3;

        int[] array = reversePrint(l1);

        for(int a : array) {
            System.out.println(a + ", ");
        }
    }
}
